(setq customary-misc-packages
      '(
        projectile
        find-file-in-project
        evil
        peep-dired
        command-log
        visual-regexp
        visual-regexp-steroids
        discover-my-major
        tiny
        ;; smartparens
        ;; markdown-mode
        expand-region
        magit
        org
        golden-ratio
        (highlight-global :location (recipe :fetcher github :repo "glen-dai/highlight-global"))
        symbol-overlay
        ranger
        (dired-mode :location built-in)
        (recentf :location built-in)
        ))


(defun customary-misc/post-init-projectile ()
  (progn
    (with-eval-after-load 'projectile
      (progn
        (setq projectile-completion-system 'ivy)
        (add-to-list 'projectile-other-file-alist '("html" "js"))
        (add-to-list 'projectile-other-file-alist '("js" "html"))))

    (defvar my-simple-todo-regex "\\<\\(FIXME\\|TODO\\|BUG\\):")

    (defun my-simple-todo ()
      "When in a project, create a `multi-occur' buffer matching the
  regex in `my-simple-todo-regex' across all buffers in the
  current project. Otherwise do `occur' in the current file."
      (interactive)
      (if (projectile-project-p)
          (multi-occur (projectile-project-buffers) my-simple-todo-regex)
        (occur my-simple-todo-regex)))
    (spacemacs/set-leader-keys "pt" 'my-simple-todo)))


;; (defun customary-misc/post-init-evil-escape ()
;;  (setq evil-escape-delay 0.2))


(defun customary-misc/init-find-file-in-project ()
  (use-package find-file-in-project
    :defer t
    :config
    (progn
      ;; If you use other VCS (subversion, for example), enable the following option
      ;;(setq ffip-project-file ".svn")
      ;; in MacOS X, the search file command is CMD+p
      ;; for this project, I'm only interested certain types of files
      (setq-default ffip-patterns '("*.html" "*.js" "*.css" "*.java" "*.xml" "*.cpp" "*.h" "*.c" "*.mm" "*.m" "*.el"))
      ;; if the full path of current file is under SUBPROJECT1 or SUBPROJECT2
      ;; OR if I'm reading my personal issue track document,
      (defadvice find-file-in-project (before my-find-file-in-project activate compile)
        (when (ffip-current-full-filename-match-pattern-p "\\(HLMJ_js\\)")
          ;; set the root directory into "~/projs/PROJECT_DIR"
          (setq-local ffip-project-root "~/Github/HLMJ_js")
          ;; well, I'm not interested in concatenated BIG js file or file in dist/
          (setq-local ffip-find-options "-not -size +64k -not -iwholename '*/bin/*'")
          ;; do NOT search files in below directories, the default value is better.
          (setq-default ffip-prune-patterns '(".git" ".hg" "*.svn" "node_modules" "bower_components" "temp"))))
      (ad-activate 'find-file-in-project))))


(defun customary-misc/init-peep-dired ()
  ;;preview files in dired
  (use-package peep-dired
    :defer t
    :commands (peep-dired-next-file
               peep-dired-prev-file)
    :bind (:map dired-mode-map
                ("P" . peep-dired))))


(defun customary-misc/post-init-command-log ()
  (with-eval-after-load 'global-command-log-mode
    (setq clm/log-command-exceptions* (append clm/log-command-exceptions*
                                              '(evil-next-visual-line
                                                evil-previous-visual-line)))))


(defun customary-misc/init-visual-regexp ()
  (use-package visual-regexp
    :commands (vr/replace vr/query-replace)))

(defun customary-misc/init-visual-regexp-steroids ()
  (use-package visual-regexp-steroids
    :commands (vr/select-replace vr/select-query-replace)
    :init
    (progn
      (define-key global-map (kbd "C-c r") 'vr/replace)
      (define-key global-map (kbd "C-c q") 'vr/query-replace))))


(defun customary-misc/post-init-smartparens ()
  :post-init
  (progn
    (defun wrap-sexp-with-new-round-parens ()
      (interactive)
      (insert "()")
      (backward-char)
      (sp-forward-slurp-sexp))

    (global-set-key (kbd "C-(") 'wrap-sexp-with-new-round-parens))

  :post-config
  (progn
    (setq sp-highlight-pair-overlay nil)

    (evil-define-key 'normal sp-keymap
      (kbd ")>") 'sp-forward-slurp-sexp
      (kbd ")<") 'sp-forward-barf-sexp
      (kbd "(>") 'sp-backward-barf-sexp
      (kbd "(<") 'sp-backward-slurp-sexp)))


(defun customary-misc/post-init-helm ()
  :post-config
  ;; limit max number of matches displayed for speed
  (setq helm-candidate-number-limit 128)
  ;; ignore boring files like .o and .a
  (setq helm-ff-skip-boring-files t)
  ;; replace locate with spotlight on Mac
  (setq helm-locate-command "mdfind -name %s %s")
  (push "\\.emlx$" helm-boring-file-regexp-list))


(defun customary-misc/post-init-osx-dictionary ()
  :post-init
  (progn
    (evilified-state-evilify osx-dictionary-mode osx-dictionary-mode-map)
    (setq osx-dictionary-use-chinese-text-segmentation t)
    (global-set-key (kbd "C-c d") 'osx-dictionary-search-pointer)
    ))


(defun customary-misc/post-init-discover-my-major ()
  :post-config
  (progn
    (spacemacs/set-leader-keys (kbd "mhm") 'discover-my-major)
    (evilified-state-evilify makey-key-mode makey-key-mode-get-key-map)))


(defun customary-misc/post-init-evil ()
  :post-config
  (progn
    (setcdr evil-insert-state-map nil)
    (define-key evil-insert-state-map [escape] 'evil-normal-state)

    (push "TAGS" spacemacs-useless-buffers-regexp)

    ;; ;; change evil initial mode state
    ;; (loop for (mode . state) in
    ;;       '((shell-mode . normal)
    ;;         (minibuffer-inactive-mode . emacs))
    ;;       do (evil-set-initial-state mode state))

    ;; (add-hook 'edebug-mode-hook '(lambda () (if edebug-mode
    ;;                                             (evil-emacs-state)
    ;;                                           (evil-normal-state))))

    ;;mimic "nzz" behaviou in vim
    (defadvice evil-ex-search-next (after advice-for-evil-search-next activate)
      (evil-scroll-line-to-center (line-number-at-pos)))

    (defadvice evil-ex-search-previous (after advice-for-evil-search-previous activate)
      (evil-scroll-line-to-center (line-number-at-pos)))

    (define-key evil-normal-state-map (kbd ",/") 'evilnc-comment-or-uncomment-lines)
    (define-key evil-normal-state-map (kbd ",te") 'spacemacs/helm-find-files)

    (define-key evil-normal-state-map
      (kbd "Y") 'customary/yank-to-end-of-line)

    ;; rebind g,k to gj and gk
    ;; (define-key evil-normal-state-map (kbd "j") 'evil-next-visual-line)
    ;; (define-key evil-normal-state-map (kbd "k") 'evil-previous-visual-line)

    (define-key evil-normal-state-map (kbd "[ SPC") (lambda () (interactive) (evil-insert-newline-above) (forward-line)))
    (define-key evil-normal-state-map (kbd "] SPC") (lambda () (interactive) (evil-insert-newline-below) (forward-line -1)))

    (define-key evil-normal-state-map (kbd "[ b") 'previous-buffer)
    (define-key evil-normal-state-map (kbd "] b") 'next-buffer)

    (define-key evil-emacs-state-map (kbd "M-f") 'forward-word)
    (define-key evil-insert-state-map (kbd "M-f") 'forward-word)
    (define-key evil-emacs-state-map (kbd "M-b") 'backward-word)
    (define-key evil-insert-state-map (kbd "M-b") 'backward-word)

    (spacemacs/set-leader-keys "bi" 'ibuffer)
    (define-key evil-ex-completion-map "\C-a" 'move-beginning-of-line)
    (define-key evil-ex-completion-map "\C-b" 'backward-char)
    (define-key evil-ex-completion-map "\C-k" 'kill-line)
    (define-key minibuffer-local-map (kbd "C-w") 'evil-delete-backward-word)

    (define-key evil-visual-state-map (kbd ",/") 'evilnc-comment-or-uncomment-lines)
    ;; (define-key evil-visual-state-map (kbd "x") 'er/expand-region)
    ;; (define-key evil-visual-state-map (kbd "X") 'er/contract-region)
    (define-key evil-visual-state-map (kbd "C-r") 'customary/evil-quick-replace)

    ;; in spacemacs, we always use evilify better-defaultsro state
    (evil-add-hjkl-bindings package-menu-mode-map 'emacs)

    ;; (define-key evil-emacs-state-map (kbd "C-w h") 'evil-window-left)
    (define-key evil-emacs-state-map (kbd "C-w") 'evil-delete-backward-word)
    ;; (define-key evil-emacs-state-map (kbd "C-w j") 'evil-window-down)
    ;; (define-key evil-emacs-state-map (kbd "C-w k") 'evil-window-up)
    ;; (define-key evil-emacs-state-map (kbd "C-w l") 'evil-window-right)


    (define-key evil-emacs-state-map (kbd "C-'") 'undo-tree-redo)
    (define-key evil-emacs-state-map (kbd "C-<") 'set-mark-command)
    (define-key evil-emacs-state-map (kbd "C-c f") 'customary/go-to-char)

    ;; for emacs shell mode
    (evil-define-key 'emacs term-raw-map (kbd "C-w")
      'evil-delete-backward-word)

    ;; (setq evil-normal-state-tag   (propertize "[N]" 'face '((:background "DarkGoldenrod2" :foreground "black")))
    ;;       evil-emacs-state-tag    (propertize "[E]" 'face '((:background "SkyBlue2" :foreground "black")))
    ;;       evil-insert-state-tag   (propertize "[I]" 'face '((:background "chartreuse3") :foreground "white"))
    ;;       evil-motion-state-tag   (propertize "[M]" 'face '((:background "plum3") :foreground "white"))
    ;;       evil-visual-state-tag   (propertize "[V]" 'face '((:background "gray" :foreground "black")))
    ;;       evil-operator-state-tag (propertize "[O]" 'face '((:background "purple"))))

    (define-key evil-insert-state-map (kbd "C-z") 'evil-emacs-state)


    (evil-define-key 'insert comint-mode-map
      (kbd "C-k") 'kill-line)
    (evil-define-key 'normal comint-mode-map
      (kbd "C-k") 'kill-line)
    )
  )


(defun customary-misc/init-tiny ()
  (use-package tiny
    :defer t
    :init
    (spacemacs/set-leader-keys "oe" 'tiny-expand)))


(defun customary-misc/init-visual-regexp ()
  (use-package visual-regexp
    :commands (vr/replace vr/query-replace)))


(defun customary-misc/init-visual-regexp-steroids ()
  (use-package visual-regexp-steroids
    :commands (vr/select-replace vr/select-query-replace)
    :init
    (progn
      (define-key global-map (kbd "C-c r") 'vr/replace)
      (define-key global-map (kbd "C-c q") 'vr/query-replace))))


(defun customary-misc/init-multiple-cursors ()
  (use-package multiple-cursors
    :init
    (progn

      (bind-key* "C-s-l" 'mc/edit-lines)
      (bind-key* "C-s-f" 'mc/mark-all-dwim)
      (bind-key* "s-." 'mc/mark-next-like-this)
      (bind-key* "C-s-," 'mc/mark-previous-like-this)
      (bind-key* "s->" 'mc/unmark-next-like-this)
      (bind-key* "s-<" 'mc/unmark-previous-like-this)
      (bind-key* "C-c C-s-." 'mc/mark-all-like-this)

      ;; http://endlessparentheses.com/multiple-cursors-keybinds.html?source=rss
      (define-prefix-command 'endless/mc-map)
      ;; C-x m is usually `compose-mail'. Bind it to something
      ;; else if you use this command.
      (define-key ctl-x-map "m" 'endless/mc-map)
;;; Really really nice!
      (define-key endless/mc-map "i" #'mc/insert-numbers)
      (define-key endless/mc-map "h" #'mc-hide-unmatched-lines-mode)
      (define-key endless/mc-map "a" #'mc/mark-all-like-this)

;;; Occasionally useful
      (define-key endless/mc-map "d" #'mc/mark-all-symbols-like-this-in-defun)
      (define-key endless/mc-map "r" #'mc/reverse-regions)
      (define-key endless/mc-map "s" #'mc/sort-regions)
      (define-key endless/mc-map "l" #'mc/edit-lines)
      (define-key endless/mc-map "\C-a" #'mc/edit-beginnings-of-lines)
      (define-key endless/mc-map "\C-e" #'mc/edit-ends-of-lines)
      )
    :config
    (setq mc/cmds-to-run-once
          '(
            counsel-M-x
            customary/my-mc-mark-next-like-this))
    (setq mc/cmds-to-run-for-all
          '(
            electric-newline-and-maybe-indent
            hungry-delete-backward
            spacemacs/backward-kill-word-or-region
            spacemacs/smart-move-beginning-of-line
            evil-substitute
            lispy-move-beginning-of-line
            lispy-move-end-of-line
            lispy-space
            lispy-delete-backward
            evil-exit-visual-state
            evil-backward-char
            evil-delete-char
            evil-escape-emacs-state
            evil-escape-insert-state
            mwim-beginning-of-code-or-line
            mwim-end-of-line-or-code
            evil-exit-emacs-state
            evil-previous-visual-line
            evil-next-visual-line
            evil-forward-char
            evil-insert
            evil-next-line
            evil-normal-state
            evil-previous-line
            evil-append
            evil-append-line
            forward-sentence
            kill-sentence
            org-self-insert-command
            sp-backward-delete-char
            sp-delete-char
            sp-remove-active-pair-overlay
            orgtbl-hijacker-command-109))
    )
  )


(defun customary-misc/init-markdown-mode ()
  (progn
    (add-to-list 'auto-mode-alist '("\\.mdown\\'" . markdown-mode))

    (with-eval-after-load 'markdown-mode
      (progn
        ;; (when (configuration-layer/package-usedp 'company)
        ;;   (spacemacs|add-company-hook markdown-mode))

        (spacemacs/set-leader-keys-for-major-mode 'gfm-mode-map
          "p" 'customary/markdown-to-html)
        (spacemacs/set-leader-keys-for-major-mode 'markdown-mode
          "p" 'customary/markdown-to-html)

        (evil-define-key 'normal markdown-mode-map (kbd "TAB") 'markdown-cycle)
        ))
    )
  )


(defun customary-misc/post-init-company ()
  :post-config
  (progn
    (setq company-minimum-prefix-length 1
          company-idle-delay 0.08)

    (when (configuration-layer/package-usedp 'company)
      (spacemacs|add-company-hook shell-script-mode)
      (spacemacs|add-company-hook makefile-bsdmake-mode)
      (spacemacs|add-company-hook sh-mode)
      (spacemacs|add-company-hook lua-mode)
      (spacemacs|add-company-hook nxml-mode)
      (spacemacs|add-company-hook conf-unix-mode)
      )
    )
  )


(defun customary-misc/post-init-ibuffer ()
  :post-config
  (progn
    (require 'projectile)
    (require 'f)

    (setq ibuffer-never-show-predicates
          `("^\\*inferior-ensime"
            "^\\*ensime-update"
            "^\\*Completions"
            "^\\*helm"
            "^\\*ivy"))

    (setq ibuffer-show-empty-filter-groups nil)

    ;; (setq ibuffer-saved-filter-groups
    ;;       (let ((files-by-project
    ;;              (--map `(,(f-filename it) (filename . ,(f-expand it))) projectile-known-projects)))
    ;;         `(("default"
    ;;            ,@files-by-project
    ;;            ("dired" (mode . dired-mode))
    ;;            ("emacs" (or (name . "\\*Messages\\*")
    ;;                         (name . "\\*Compile-Log\\*")
    ;;                         (name . "\\*scratch\\*")
    ;;                         (name . "\\*Backtrace\\*")
    ;;                         (name . "\\*spacemacs\\*")
    ;;                         (name . "\\*emacs\\*")))
    ;;            ("help" (name . "\\*Help\\*"))))))

    ;; (add-hook 'ibuffer-hook
    ;;           (lambda ()
    ;;             (ibuffer-auto-mode 1)
    ;;             (ibuffer-switch-to-saved-filter-groups "default")))


    (defface customary/ibuffer-common-face
      '((default
          :inherit font-lock-keyword-face
          :underline nil
          :background "yellow"))
      "Face used for ibuffer common buffers")

    (defface customary/ibuffer-shell-term-face
      '((default
          :inherit font-lock-keyword-face
          :underline nil))
      "Face used for ibuffer shell/term/eshell buffers")

    (defface customary/ibuffer-common-modified-face
      '((default
          :inherit customary/ibuffer-common-face
          :weight bold
          ))
      "Face used for ibuffer common but modifed buffers")

    (defface customary/ibuffer-star-face
      '((default
          :inherit font-lock-preprocessor-face
          :slant italic))
      "Face used for star-stared buffers")

    (defface customary/ibuffer-readonly-face
      '((default
          :inherit font-lock-constant-face))
      "Face used for readonly buffers")

    (defface customary/ibuffer-compressed-file-face
      '((default
          :inherit font-lock-doc-face))
      "Face used for compressed file buffers")

    (defface customary/ibuffer-null-face
      '((default
          :slant italic))
      "Face used for null buffers")

    (defface customary/ibuffer-help-buffer-mode-face
      '((default
          :inherit font-lock-comment-face
          :foreground "green"
          ))
      "Face used for help buffer modes")

    (defface customary/ibuffer-dired-mode-face
      '((default
          :inherit font-lock-function-name-face
          ))
      "Face used for dired modes")

    (defconst customary/ibuffer-fontification-alist
      '((ruby-mode . font-lock-string-face)
        (sh-mode . font-lock-string-face)
        (objc-mode . font-lock-constant-face)
        (c-mode . font-lock-constant-face)
        (java-mode . font-lock-constant-face)
        (emacs-lisp-mode . font-lock-variable-name-face)
        (org-mode . font-lock-negation-char-face)
        (dired-mode . font-lock-function-name-face)
        (term-mode . font-lock-doc-string-face)))

    (setq ibuffer-fontification-alist
          ;; `(,@(mapcar (lambda (b)
          ;;               `(999 (eq major-mode ',(car b)) ,(cdr b)))
          ;;             customary/ibuffer-fontification-alist)
          '(

            ;; (20
            ;;  (and buffer-file-name
            ;;       (buffer-modified-p (buffer-file-name)))
            ;;  customary/ibuffer-common-modified-face)

            (25
             buffer-file-name
             customary/ibuffer-common-face)

            (30
             buffer-read-only
             customary/ibuffer-readonly-face)

            (35
             (and buffer-file-name
                  (string-match ibuffer-compressed-file-name-regexp buffer-file-name))
             customary/ibuffer-compressed-file-face)

            (40
             (string-match "^*"
                           (buffer-name))
             customary/ibuffer-star-face)

            (42
             (or (eq major-mode 'shell-mode)
                 (eq major-mode 'eshell-mode)
                 (eq major-mode 'term-mode))
             customary/ibuffer-shell-term-face
             )

            (45
             (and
              (string-match "^ "
                            (buffer-name))
              (null buffer-file-name))
             customary/ibuffer-null-face)

            (50
             (memq major-mode ibuffer-help-buffer-modes)
             customary/ibuffer-help-buffer-mode-face)

            (55
             (derived-mode-p
              (quote dired-mode))
             customary/ibuffer-dired-mode-face)))

    ;; Use human readable Size column instead of original one
    (define-ibuffer-column size-h
      (:name "Size" :inline t)
      (cond
       ((> (buffer-size) 1000000) (format "%7.1fM" (/ (buffer-size) 1000000.0)))
       ;; ((> (buffer-size) 100000) (format "%7.0fk" (/ (buffer-size) 1000.0)))
       ((> (buffer-size) 1000) (format "%7.1fk" (/ (buffer-size) 1000.0)))
       (t (format "%8d" (buffer-size)))))

    ;; Modify the default ibuffer-formats
    (setq ibuffer-formats
          '((mark modified read-only " "
                  (name 18 18 :left :elide)
                  " "
                  (size-h 9 -1 :right)
                  " "
                  (mode 16 16 :left :elide)
                  " "
                  filename-and-process)))
    )
  )

(defun customary-misc/post-init-expand-region ()
  (with-eval-after-load 'expand-region
    (when (configuration-layer/package-used-p 'helm-ag)
      (defadvice er/prepare-for-more-expansions-internal
          (around helm-ag/prepare-for-more-expansions-internal activate)
        ad-do-it
        (let ((new-msg (concat (car ad-return-value)
                               ", H to highlight in buffers"
                               ", / to search in project, "
                               "f to search in files, "
                               "b to search in opened buffers"))
              (new-bindings (cdr ad-return-value)))
          (cl-pushnew
           '("H" (lambda ()
                   (call-interactively
                    'customary/highlight-dwim)))
           new-bindings)
          (cl-pushnew
           '("/" (lambda ()
                   (call-interactively
                    'spacemacs/helm-project-smart-do-search-region-or-symbol)))
           new-bindings)
          (cl-pushnew
           '("f" (lambda ()
                   (call-interactively
                    'spacemacs/helm-files-smart-do-search-region-or-symbol)))
           new-bindings)
          (cl-pushnew
           '("b" (lambda ()
                   (call-interactively
                    'spacemacs/helm-buffers-smart-do-search-region-or-symbol)))
           new-bindings)
          (setq ad-return-value (cons new-msg new-bindings))))))
  )


(defun customary-misc/post-init-magit ()
  (progn
    (with-eval-after-load 'magit
      (progn

        (add-to-list 'magit-no-confirm 'stage-all-changes)
        (define-key magit-log-mode-map (kbd "W") 'magit-copy-section-value)
        (setq magit-completing-read-function 'magit-builtin-completing-read)

        (magit-define-popup-switch 'magit-push-popup ?u
                                   "Set upstream" "--set-upstream")


        (magit-add-section-hook 'magit-status-sections-hook
                                'magit-insert-assume-unchanged-files nil t)

        ;; insert the hidden files section in the magit status buffer.
        (magit-add-section-hook 'magit-status-sections-hook
                                'magit-insert-skip-worktree-files nil t)

        (define-key magit-status-mode-map "ga" 'magit-jump-to-assume-unchanged)
        (define-key magit-status-mode-map "gw" 'magit-jump-to-skip-worktree)
        ))

    ;; prefer two way ediff
    (setq magit-ediff-dwim-show-on-hunks t)

    ;; (setq magit-repository-directories '("~/cocos2d-x/"))
    (setq magit-push-always-verify nil)

    (eval-after-load 'magit
      '(define-key magit-mode-map (kbd "C-c g")
         #'customary/magit-visit-pull-request))

    (setq magit-process-popup-time 10)))

(defun customary-misc/pre-init-org ()
  ;; (setq org-ellipsis "")
  (setq org-priority-faces
        '((65 :inherit org-priority :foreground "red")
          (66 :inherit org-priority :foreground "brown")
          (67 :inherit org-priority :foreground "blue")))
  (setq org-structure-template-alist
        '(("n" "#+NAME: ?")
          ("L" "#+LaTeX: ")
          ("h" "#+HTML: ")
          ("q" "#+BEGIN_QUOTE\n\n#+END_QUOTE")
          ("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
          ("se" "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC")
          ("sp" "#+BEGIN_SRC python\n\n#+END_SRC")))

  (add-hook 'org-mode-hook (lambda () (auto-fill-mode 1)))
  (add-hook 'org-mode-hook 'flyspell-mode)

  ;; Experimenting with the following indentation vars:
  (setq org-startup-indented nil)
  (setq org-hide-leading-stars t)
  (setq org-hide-emphasis-markers nil)
  (setq org-indent-indentation-per-level 1))

(defun customary-misc/post-init-org ()
  (evil-define-key '(normal visual motion) org-mode-map
    "gh" 'outline-up-heading
    "gj" 'outline-forward-same-level
    "gk" 'outline-backward-same-level
    "gl" 'outline-next-visible-heading
    "gu" 'outline-previous-visible-heading)

  (spacemacs/set-leader-keys "aof" 'org-open-at-point-global)

  (spacemacs/set-leader-keys-for-major-mode 'org-mode
    "r" 'org-refile
    "h" 'org-metaleft  ; Because of MacOS's damned, indestructable M-h binding...
    "s p" 'org-sort-entries-priorities))


(defun customary-misc/init-highlight-global ()
  (use-package highlight-global
    :init
    (progn

      (setq-default highlight-faces
                    '(('hi-red-b . 0)
                      ('hi-yellow . 0)
                      ('hi-pink . 0)
                      ('hi-blue-b . 0))))))


(defun customary-misc/post-init-symbol-overlay ()
  (with-eval-after-load 'symbol-overlay
    (progn
      (spacemacs/transient-state-register-add-bindings 'symbol-overlay
        '((">" symbol-overlay-jump-last)
          ("s" spacemacs/swiper-region-or-symbol)
          ("<" symbol-overlay-jump-first))))))


(defun customary-misc/post-init-golden-ratio ()
  (with-eval-after-load 'golden-ratio
    (dolist (mode '("dired-mode" "occur-mode"))
      (add-to-list 'golden-ratio-exclude-modes mode))
    (dolist (n '("COMMIT_EDITMSG"))
      (add-to-list 'golden-ratio-exclude-buffer-names n))))


(defun customary-misc/pre-init-ranger ()
  ;; https://emacs-china.org/t/ranger-golden-ratio/964/2
  (defun my-ranger ()
    (interactive)
    (if golden-ratio-mode
        (progn
          (golden-ratio-mode -1)
          (ranger)
          (setq golden-ratio-previous-enable t))
      (progn
        (ranger)
        (setq golden-ratio-previous-enable nil))))

  (defun my-quit-ranger ()
    (interactive)
    (if golden-ratio-previous-enable
        (progn
          (ranger-close)
          (golden-ratio-mode 1))
      (ranger-close)))

  (with-eval-after-load 'ranger
    (progn
      (define-key ranger-normal-mode-map (kbd "q") 'my-quit-ranger)))

  (spacemacs/set-leader-keys "ar" 'my-ranger))


(defun customary-misc/post-init-recentf ()
  (progn
    (setq recentf-exclude
          '("COMMIT_MSG"
            "COMMIT_EDITMSG"
            "github.*txt$"
            "/tmp/"
            "/ssh:"
            "/sudo:"
            "/TAGS$"
            "/GTAGS$"
            "/GRAGS$"
            "/GPATH$"
            "\\.mkv$"
            "\\.mp[34]$"
            "\\.avi$"
            "\\.pdf$"
            "\\.sub$"
            "\\.srt$"
            "\\.ass$"
            ".*png$"))
    (setq recentf-max-saved-items 2048)))


(defun customary-misc/init-dired-mode ()
  (use-package dired-mode
    :defer t
    :init
    (progn
      (require 'dired-x)
      (require 'dired-aux)
      (add-hook 'dired-mode-hook 'dired-async-mode)
      ;; (setq dired-listing-switches "-alh")
      (setq dired-listing-switches (if (eq system-type 'berkeley-unix) "-alh" "-alhv"))
      (setq dired-guess-shell-alist-user
            '(("\\.pdf\\'" "open")
              ("\\.docx\\'" "open")
              ("\\.\\(?:djvu\\|eps\\)\\'" "open")
              ("\\.\\(?:jpg\\|jpeg\\|png\\|gif\\|xpm\\)\\'" "open")
              ("\\.\\(?:xcf\\)\\'" "open")
              ("\\.csv\\'" "open")
              ("\\.tex\\'" "open")
              ("\\.\\(?:mp4\\|mkv\\|avi\\|flv\\|ogv\\)\\(?:\\.part\\)?\\'"
               "open")
              ("\\.\\(?:mp3\\|flac\\)\\'" "open")
              ("\\.html?\\'" "open")
              ("\\.md\\'" "open")))

      (setq dired-omit-files
            (concat dired-omit-files "\\|^.DS_Store$\\|^.projectile$\\|\\.js\\.meta$\\|\\.meta$"))

      ;; always delete and copy recursively
      (setq dired-recursive-deletes 'always)
      (setq dired-recursive-copies 'always)
      (setq dired-dwim-target t)

      (defun ora-ediff-files ()
        (interactive)
        (let ((files (dired-get-marked-files))
              (wnd (current-window-configuration)))
          (if (<= (length files) 2)
              (let ((file1 (car files))
                    (file2 (if (cdr files)
                               (cadr files)
                             (read-file-name
                              "file: "
                              (dired-dwim-target-directory)))))
                (if (file-newer-than-file-p file1 file2)
                    (ediff-files file2 file1)
                  (ediff-files file1 file2))
                (add-hook 'ediff-after-quit-hook-internal
                          (lambda ()
                            (setq ediff-after-quit-hook-internal nil)
                            (set-window-configuration wnd))))
            (error "no more than 2 files should be marked"))))

      (define-key dired-mode-map "e" 'ora-ediff-files)

      )))
